package com.shanzmoo.admin.web;

import com.shanzmoo.base.bean.PageRespBase;
import com.shanzmoo.base.bean.SHttpResponse;
import com.shanzmoo.base.bean.SResponse;
import com.shanzmoo.base.constant.Constant;
import com.shanzmoo.core.domain.OutletOrderSaveDto;
import com.shanzmoo.core.manager.OutletOrderManager;
import com.shanzmoo.db.order.entity.bo.PayMoneyResultBO;
import com.shanzmoo.db.order.entity.dto.*;
import com.shanzmoo.db.order.entity.vo.OutletOrderInfoVo;
import com.shanzmoo.db.order.entity.vo.OutletOrderLogVo;
import com.shanzmoo.db.order.entity.vo.OutletOrderPageVo;
import com.shanzmoo.core.domain.FactoryOrderSaveDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

/**
 * 门店订单管理
 *
 * @author: Gzhao 2020/12/14
 */
@Slf4j
@Api(tags = "门店-门店订单管理")
@RestController
@RequestMapping("/api/order")
public class OutletOrderController {

    @Autowired
    private OutletOrderManager outletOrderManager;

    @PreAuthorize("!hasAnyAuthority('admin')")
    @ApiOperation(value = "添加")
    @PostMapping("/add")
    public SHttpResponse<Boolean> add(@Valid @RequestBody OutletOrderSaveDto saveDto) {
        SResponse<Boolean> re = outletOrderManager.add(saveDto);
        return SHttpResponse.transform(re);
    }

    @ApiOperation(value = "批量导入商品")
    @PostMapping("/obtainImportSkuList")
    public SHttpResponse<List<OutletOrderDetailSaveDto>> obtainImportSkuList(@RequestParam("file") MultipartFile file) {
        if (file == null || file.isEmpty()) {
            log.error("文件上传失败，上传的文件为空！");
            return SHttpResponse.fail(Constant.ERRCODE_PARAM, "上传文件不能为空");
        }

        SResponse<List<OutletOrderDetailSaveDto>> re = outletOrderManager.obtainImportSkuList(file);
        return SHttpResponse.transform(re);
    }

    @ApiOperation(value = "分页列表")
    @PostMapping("/pageList")
    public SHttpResponse<PageRespBase<OutletOrderPageVo>> pageList(@Valid @RequestBody OutletOrderQueryDto queryDto) {
        SResponse<PageRespBase<OutletOrderPageVo>> re = outletOrderManager.getPageList(queryDto);
        return SHttpResponse.transform(re);
    }

    @ApiOperation(value = "订单详情")
    @GetMapping("/info")
    public SHttpResponse<OutletOrderInfoVo> getInfo(@ApiParam(value = "订单号") @RequestParam(value = "orderNo") String orderNo) {
        SResponse<OutletOrderInfoVo> re = outletOrderManager.getInfo(orderNo);
        return SHttpResponse.transform(re);
    }

    @ApiOperation(value = "订单商品明细")
    @GetMapping("/infoDeatils")
    public SHttpResponse<List<OutletOrderDetailInfoDto>> getInfoDeatils(@ApiParam(value = "订单号") @RequestParam(value = "orderNo") String orderNo) {
        SResponse<List<OutletOrderDetailInfoDto>> re = outletOrderManager.getInfoDeatils(orderNo);
        return SHttpResponse.transform(re);
    }

    @PreAuthorize("!hasAnyAuthority('admin')")
    @ApiOperation(value = "订单待分配明细")
    @GetMapping("/waitAllotDetail")
    public SHttpResponse<List<OutletOrderDetailInfoDto>> getWaitAllotDetail(@ApiParam(value = "订单号") @RequestParam(value = "orderNo") String orderNo) {
        SResponse<List<OutletOrderDetailInfoDto>> re = outletOrderManager.getWaitAllotDetail(orderNo);
        return SHttpResponse.transform(re);
    }

    @PreAuthorize("!hasAnyAuthority('admin')")
    @ApiOperation(value = "确认分配订单")
    @PostMapping("/confirmAllot")
    public SHttpResponse<Boolean> confirmAllot(@Valid @RequestBody FactoryOrderSaveDto saveDto) {
        SResponse<Boolean> re = outletOrderManager.confirmAllot(saveDto);
        return SHttpResponse.transform(re);
    }

    @PreAuthorize("!hasAnyAuthority('admin')")
    @ApiOperation(value = "订单作废")
    @PostMapping("/cancel")
    public SHttpResponse<Boolean> cancelOrder(@Valid @RequestBody OutletOrderCancelDto cancelDto) {
        SResponse<Boolean> re = outletOrderManager.cancelOrder(cancelDto);
        return SHttpResponse.transform(re);
    }

    @PreAuthorize("!hasAnyAuthority('admin')")
    @ApiOperation(value = "订单结算")
    @PostMapping("/financeFinish")
    public SHttpResponse<Boolean> financeFinish(@Valid @RequestBody OutletOrderFinanceFinishDto financeFinishDto) {
        SResponse<Boolean> re = outletOrderManager.financeFinish(financeFinishDto);
        return SHttpResponse.transform(re);
    }

//    @PreAuthorize("!hasAnyAuthority('admin')")
    @ApiOperation(value = "生成发货单")
    @GetMapping("/createDeliverBill")
    public SHttpResponse<Boolean> createDeliverBill(
            @ApiParam(value = "订单号") @RequestParam(value = "orderNo") String orderNo) {
        SResponse<Boolean> re = outletOrderManager.createDeliverBill(orderNo);
        return SHttpResponse.transform(re);
    }

    @Deprecated
    @PreAuthorize("!hasAnyAuthority('admin')")
    @ApiOperation(value = "自提发货")
    @GetMapping("/selfDeliver")
    public SHttpResponse<Boolean> selfDeliver(@ApiParam(value = "订单号") @RequestParam(value = "orderNo") String orderNo) {
        SResponse<Boolean> re = outletOrderManager.selfDeliver(orderNo);
        return SHttpResponse.transform(re);
    }

    @Deprecated
    @PreAuthorize("!hasAnyAuthority('admin')")
    @ApiOperation(value = "物流发货")
    @PostMapping("/logisticsDeliver")
    public SHttpResponse<Boolean> logisticsDeliver(@Valid @RequestBody LogisticsDeliverDto deliverDto) {
        SResponse<Boolean> re = outletOrderManager.logisticsDeliver(deliverDto);
        return SHttpResponse.transform(re);
    }

    @PreAuthorize("!hasAnyAuthority('admin')")
    @ApiOperation(value = "扫描用户支付码支付订单")
    @PostMapping("/payMoney")
    public SHttpResponse<PayMoneyResultBO> payMoney(@Valid @RequestBody OrderPayDTO payDTO) {
        SResponse<PayMoneyResultBO> re = outletOrderManager.payMoney(payDTO);
        return SHttpResponse.transform(re);
    }

    @ApiOperation("订单操作日志")
    @GetMapping("logList")
    public SHttpResponse<List<OutletOrderLogVo>> getLogList(@ApiParam("订单号") @RequestParam(value = "orderId") Integer orderId) {
        List<OutletOrderLogVo> re = outletOrderManager.getLogList(orderId);
        return SHttpResponse.ok(re);
    }

}
